package commands

import (
	"ConvertFlow/protocols/ospf"
	"fmt"
	"github.com/sirupsen/logrus"
	"github.com/spf13/cobra"
	"os"
)

// OSPFFlags OSPF协议专用的标志
type OSPFFlags struct {
	Interface     string
	RemoteAddr    string
	ListenAddr    string
	ChunkSize     int
	Delay         int
	AreaID        string
	RouterID      string
	HelloInterval int
}

var ospfFlags = &OSPFFlags{}

// OspfRootCmd OSPF主命令
var OspfRootCmd = &cobra.Command{
	Use:   "ospf",
	Short: "Commands using OSPF protocol",
	Long:  `All commands that use OSPF protocol for data transmission`,
}

// 发送命令
var ospfSendCmd = &cobra.Command{
	Use:   "send [string to send]",
	Short: "Send data using OSPF protocol",
	Long:  `Send data to a remote receiver using OSPF protocol.`,
	Args:  cobra.MinimumNArgs(1),
	Run:   runSendByOspf,
}

// 文件发送命令
var ospfSendFileCmd = &cobra.Command{
	Use:   "file [path of the file]",
	Short: "Send file content using OSPF protocol",
	Long:  `Send the content of a file to a remote receiver using OSPF protocol.`,
	Args:  cobra.ExactArgs(1),
	Run:   runSendFileByOspf,
}

func init() {
	// 添加OSPF标志
	addOSPFFlags(ospfSendCmd)

	// 构建命令层级关系
	OspfRootCmd.AddCommand(ospfSendCmd)
	ospfSendCmd.AddCommand(ospfSendFileCmd)
}

// addOSPFFlags 添加OSPF专用标志
func addOSPFFlags(cmd *cobra.Command) {
	cmd.PersistentFlags().StringVarP(&ospfFlags.ListenAddr, "listen", "l", "192.168.0.245", "OSPF listen address")
	cmd.PersistentFlags().StringVarP(&ospfFlags.RemoteAddr, "remote", "r", "192.168.0.243", "OSPF remote address (required)")
	cmd.PersistentFlags().StringVarP(&ospfFlags.Interface, "interface", "c", "ens34", "OSPF Interface")
	cmd.PersistentFlags().IntVarP(&ospfFlags.ChunkSize, "size", "s", 256, "OSPF packet size")
	cmd.PersistentFlags().IntVarP(&ospfFlags.Delay, "delay", "d", 1, "OSPF packet delay")
	cmd.PersistentFlags().StringVarP(&ospfFlags.AreaID, "area", "a", "0", "OSPF area ID")
	cmd.PersistentFlags().StringVarP(&ospfFlags.RouterID, "router-id", "i", "", "OSPF router ID")
	cmd.PersistentFlags().IntVarP(&ospfFlags.HelloInterval, "hello", "H", 10, "OSPF hello interval")
	cmd.MarkFlagRequired("remote")
}

// runSendByOspf 处理发送字符串的逻辑
func runSendByOspf(cmd *cobra.Command, args []string) {
	// 打印调试信息
	fmt.Printf("Sending data using OSPF:\n")
	fmt.Printf("  Hello Interval: %d\n", ospfFlags.HelloInterval)
	var (
		data string
	)

	data = args[0]
	if len(data) < 0 {
		logrus.Error("runSendByOspf function failed, the content is empty!")
		return
	}

	name := "ens34"
	srcMac := "00:0c:29:70:02:f7" // 源MAC地址
	dstMac := "34:4B:50:00:00:00" // 目的MAC地址
	ospf.SendNoReply(name, srcMac, dstMac, ospfFlags.ListenAddr, ospfFlags.RemoteAddr, ospfFlags.ChunkSize, ospfFlags.Delay, data)
}

// runSendFileByOspf 处理发送文件的逻辑
func runSendFileByOspf(cmd *cobra.Command, args []string) {
	var (
		data string
	)

	//文件路径
	filePath := args[0]

	// 读取文件内容
	content, err := os.ReadFile(filePath)
	if err != nil {
		logrus.Error("Error reading file: %v\n", err)
		os.Exit(1)
	}

	data = string(content)
	if len(data) < 0 {
		logrus.Error("runSendFileByOspf function failed, the content is empty!")
		return
	}

	name := "ens34"
	srcMac := "00:0c:29:70:02:f7" // 源MAC地址
	dstMac := "34:4B:50:00:00:00" // 目的MAC地址
	ospf.SendNoReply(name, srcMac, dstMac, ospfFlags.ListenAddr, ospfFlags.RemoteAddr, ospfFlags.ChunkSize, ospfFlags.Delay, data)
}
